from ddt import ddt, data, unpack
from unittest import TestCase
from selenium import webdriver
from AddCardData import AddCardData
from AddCardOperation import AddCardOperation
from DBUtils import DBUtils


@ddt
class TestAddCard(TestCase):
    @data(*AddCardData.addcard_data)
    @unpack
    def testAddCard(self, casename, username, password, bankname, cardtype, cardnum, expect):
        # 开始测试
        driver = webdriver.Chrome()
        # 打开浏览器页面并最大化
        driver.get("http://localhost:90")
        driver.maximize_window()

        dbutils = DBUtils()
        before_result = dbutils.select('select cardNum from bankcard')
        num = len(before_result)
        # 执行注册
        addcard = AddCardOperation()
        addcard.addcard(driver, username, str(int(password)), bankname, str(int(cardtype)), cardnum)
        # 获取注册成功实际结果
        result = addcard.addcard_result(cardnum, num)
        if result != int(expect):
            driver.save_screenshot(casename + '.jpg')
        else:
            print('通过')

        driver.quit()

        self.assertEqual(result, int(expect))
